package schedule;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import common.Service;
import entities.TWebsite;
import interfaces.IGlobalService;
import interfaces.IWebsiteService;
import util.LogUtil;
import utils.DateUtil;

public class WebsiteSchedule implements Job {
	
	private IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
	private IWebsiteService websiteService = (IWebsiteService) Service.create(IWebsiteService.class);

	public void execute(JobExecutionContext context) throws JobExecutionException {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 过期网站清理任务启动");
		
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" select web.web_no 'webNo' , web.web_address 'webAddress' ");
			sb.append(" from t_website web ");
			sb.append(" where web.web_state='1' and  datediff(curdate(),web.web_vdate)>web.web_validity ");
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sb.toString());
			querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			List<LinkedHashMap<String, Object>> list = resultSet.getResultList();
			LogUtil.schedule("WEBSITE", "今日过期网站共 " + list.size() + " 个");
			for (LinkedHashMap<String, Object> map : list) {
				TWebsite website = new TWebsite();
				website.setWebNo(map.get("webNo").toString());
				website.setWebState("0");
				website.setWebVstate("0");
				websiteService.persist(website);
				LogUtil.schedule("WEBSITE", map.get("webNo").toString() + "|" + map.get("webAddress").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public static void main(String[] args) {
		IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
		IWebsiteService websiteService = (IWebsiteService) Service.create(IWebsiteService.class);
		
		System.out.println("websiteSchedule");
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" select web.web_no 'webNo' , web.web_address 'webAddress' ");
			sb.append(" from t_website web ");
			sb.append(" where web.web_state='1' and  datediff(curdate(),web.web_vdate)>web.web_validity ");
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sb.toString());
			querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			List<LinkedHashMap<String, Object>> list = resultSet.getResultList();
			LogUtil.schedule("WEBSITE", "今日过期网站共 " + list.size() + " 个");
			for (LinkedHashMap<String, Object> map : list) {
				TWebsite website = new TWebsite();
				website.setWebNo(map.get("webNo").toString());
				website.setWebState("0");
				website.setWebVstate("0");
				websiteService.persist(website);
				LogUtil.schedule("WEBSITE", map.get("webNo").toString() + "|" + map.get("webAddress").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
